<?php
/**
 * Users controller class
 *
 */

class AuctionsController extends Admin_Controller
{
    //private $table_tags;

	public function init()
    {
    	parent::init();
    	$this->_table = new Admin_Table_Auctions();
    	$this->setActiveCat(977);

    	$this->_table_field = 'id_auction'; // for table objects
    }

    /**
     * index action method
     *
     */
    public function indexAction()
    {
    	$this->page_type = 'list';

    	$reset = $this->_request->getParam('reset', '');
    	$popular = $this->_request->getParam('popular', 0);

    	$form = new Llib_Form($this->_request);
    	$data = array();

    	if ($this->_request->isPost())
    	{
	    	$data['is_active'] = $this->_request->getParam('is_active', -1);
	    	$data['is_top'] = $this->_request->getParam('is_top', -1);
	    	//$data['id_cat'] = $this->_request->getParam('id_cat', -1);
	    	//$data['id_lang'] = $this->_request->getParam('id_lang', -1);
	    	$data['s'] = $this->_request->getParam('s', '');
    	}

        $params = $this->SessionVars($data, array('is_active', 'is_top', 's'), $reset);

    	$lclm = new Llib_List($this->_request, $this->_sess);
    	$lclm->AddParams(array('checkboxes' => true));
    	$lclm->AddColumn('Заголовок', 'title', array('width' => '60%', 'sort' => 1));
    	//$lclm->AddColumn('Категория', 'menu_name_ru', 'id_cat', array('width' => '20%', 'sort' => 1));
    	$lclm->AddColumn('Дата публіковання', 'public_date', array('width' => '10%', 'sort' => 1));

    	$lclm->AddFunc(array('title' => 'Головна', 'name' => 'top', 'field' => 'is_top'));
    	$lclm->AddFunc(array('title' => 'Опубліковано', 'name' => 'public', 'field' => 'is_public'));
    	$lclm->AddFunc();

    	$lclm->AddData($this->_table->GetDataList($lclm->page, $lclm->s, $lclm->pager_count, $params));

        $data['is_active'] = $form->GetActiveList(@$params['is_active']);
        $data['is_top'] = $form->GetTopList(@$params['is_top']);
        //$data['id_cat'] = $this->table_categories->GetCatList(array($params['id_cat']), array('id_type' => 3));
        //$data['id_lang'] = $this->_table->GetLangList(@$params['id_lang']);
        $data['s'] = $params['s'];

        $form->AddData($data);

        $this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->assign('List', $lclm->GetList());
	        $this->view->assign($form->GetFrom());
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }

    public function editAction()
    {
    	$id = $this->_getParam('id');
    	$saveexit = $this->_getParam('saveexit');
    	if(!($id > 0)) $this->_redirect('/error/e404');

    	$data = $this->_table->GetDataRow($id);

    	$form = new Llib_Form($this->_request);

        if ($this->_request->isPost())
        {
			$d = $form->GetPostedData(array('tags'));
			$tags = $this->_request->getParam('tags');
			//$this->table_tags->insertStr($tags, $this->controller, $id, $this->_user->id);
        	//$this->LoadAvatar($id, $form, $d);

        	if(!isset($d['is_idt'])) $d['idt'] = $this->tstring($d['title']);

			if($this->_table->update($d, $id, $this->_user->id))
			{
			   if(is_null($saveexit))  $form->SetSuccess();
			   else $this->_redirect('/'.$this->controller);
			}
        	else $form->SetError($this->_table->GetError());
        }

        $data = $this->_table->GetDataRow($id);
       // $data['id_cat'] = $this->table_categories->GetCatList(array($data['id_cat']), array('id_type' => 3));


		foreach ($data['public_date'] as $attributes) {
        	$data['public_day'] = $form->GetDateDayList(intval($attributes['day']));
        	$data['public_month'] = $form->GetDateMonList(intval($attributes['dmonth']));
        	$data['public_year'] = $form->GetDateYearList(intval($attributes['year']));
        	$data['public_hour'] = $form->GetDateHourList(intval($attributes['hour']));
        	$data['public_min'] = $form->GetDateMinList(intval($attributes['minute']));
        }

        unset($data['public_date']);

        //$data['id_cat'] = $this->table_categories->GetCatList(array($data['id_cat']), array('parent_id' => 62, 'id_menu' => 1));

        $form->AddData($data);

    	$this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->xmlWriter->writeAttribute('id', $id);
	        $this->view->assign($form->GetFrom());
	        $this->view->assign('photos', $this->table_objects->GetDataParamsList(array($this->_table_field => $id)));
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }

	// *****************************
    // PHOTO

    public function photosAction()
    {
    	$id = $this->_request->getParam('id');
    	$saveexit = $this->_getParam('saveexit');
    	if(!($id > 0)) $this->_redirect('/error/e404');


    	$form = new Llib_Form($this->_request);

    	if ($this->_request->isPost())
    	{
    		$d = $form->GetPostedData();
    		$this->table_objects->order_dragsort($d['list1SortOrder']);
    		if(!is_null($saveexit))$this->_redirect('/'.$this->controller);
    	}

    	$this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->xmlWriter->writeAttribute('id', $id);
	        $this->view->xmlWriter->writeAttribute('table_name', 'objects');
			$this->view->assign($form->GetFrom());
	        $this->view->assign('photos', $this->table_objects->GetDataParamsList(array($this->_table_field => $id)));
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }

    public function addphotoAction()
    {
    	$article_id = $this->_request->getParam('id');
    	if(!($article_id > 0)) $this->_redirect('/error/e404');

    	$d['id_article'] = $article_id;

		$id = $this->table_objects->insert($d, $this->_user->id);

		if($id > 0)
		{
			$this->_redirect('/'.$this->controller.'/editphoto/id/'.$id);
		}else $this->_redirect('/error/e404');
    }

	public function editphotoAction()
    {
    	$id = $this->_request->getParam('id');
    	$saveexit = $this->_getParam('saveexit');
    	if(!($id > 0)) $this->_redirect('/error/e404');

    	$data = $this->table_objects->GetDataRow($id);
    	$art = $this->_table->GetDataRow($data[$this->_table_field]);

    	$form = new Llib_Form($this->_request);

        if ($this->_request->isPost())
        {
			$d = $form->GetPostedData();

			$this->LoadAvatar($id, $form, $d, 'objects');

        	if($this->table_objects->update($d, $id, $this->_user->id)) $form->SetSuccess();
        	else $form->SetError($this->_table->GetError());

			if(!is_null($saveexit))  $this->_redirect('/'.$this->controller.'/photos/'.$art['id']);
        }

        $data = $this->table_objects->GetDataRow($id);

        $form->AddData($data);

    	$this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->xmlWriter->writeAttribute('id', $art['id']);
	        $this->view->xmlWriter->writeAttribute('title', $art['title']);
	        $this->view->assign($form->GetFrom());
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }


	public function uploadPhotosAction() {
        $result = array('success' => false, 'msg' => "");
        $id = $this->_request->getParam('id');
        $field = $this->_request->getParam('f');
        if(!(intval($id) > 0 && trim($field) != '')) return $result;

        $adapter = $this->getFileAdapterAll();
        $files = $adapter->getFileInfo();

        foreach ($files as $file => $info) {
            if ($adapter->isUploaded($file) && $adapter->isValid($file)) {

                //if ($info['type'] == "application/xml")
                {
                    $md5_file = md5_file($info['tmp_name']);

                    $data = array();
                    $data['title_en'] = $data['title_ru'] = $data['title_ua'] = $info['name'];
                    $data['md5'] = $md5_file;
                    $data[$field] = $id;

                    //print_r($data); die();

                     $id = $this->table_objects->insert($data);
                        if ($id) {
                            $path = $this->controller . DIRECTORY_SEPARATOR . intval($id / 1000000) . DIRECTORY_SEPARATOR . intval($id / 1000) . DIRECTORY_SEPARATOR . intval($id);
                            if ($this->_files->CreateFolderRecursive($path)) {

                                $ext = pathinfo($info['name']);
                                $file_name = time() . $id . "." . $ext['extension'];

                                $adapter->setDestination($this->_path_files . DIRECTORY_SEPARATOR . $path);
                                $adapter->addFilter('Rename', array(
                                    'source' => $info['tmp_name'],
                                    'target' => $this->_path_files . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $file_name,
                                    'overwrite' => true));
                                $adapter->receive();

                                $data = array();
                                $data['is_ghost'] = 0;
                                $data['path'] = $this->_path_files . DIRECTORY_SEPARATOR . $path;
                                $data['file_name'] = $file_name;
                                $this->table_objects->update($data, $id);

                                $result['msg'] = "Завантажено";
                                $result['success'] = true;
                            } else {
                                $result['msg'] = $this->_files->GetError();
                            }
                        } else {
                            @unlink($info['tmp_name']);
                            $result['msg'] = "Помилка бази даних";
                        }

                } /*else {
                    $result['msg'] = "Не вірний формат файлу";
                }*/
            }
        }

        $this->ajaxOut($result, 'json');
    }


}
